package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-19 13:27
 **/
public class LeetCode541 {
    public String reverseStr(String s, int k) {
        char[] letters = s.toCharArray();
        int low = 0;
        int high = s.length() - 1;
        while (low < high) {
            //表示当前索引尾部距当前索引的距离
            int a = high - low + 1;
            if (a >= 2 * k) {
                reverse(letters, low, low + k - 1);
                low += 2 * k;
                continue;
            } else if (a < 2 * k && a >= k) {
                reverse(letters, low, low + k - 1);
                break;
            }
            reverse(letters, low, high);
            break;
        }
        return new String(letters);
    }

    public void reverse(char[] ch, int low, int high) {
        while (low < high) {
            char temp = ch[low];
            ch[low] = ch[high];
            ch[high] = temp;
            low++;
            high--;
        }
    }





}
